package com.example.warehouse.sys.controller;

import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.security.CurrentPrincipal;
import com.example.warehouse.common.web.JsonResult;
import com.example.warehouse.core.excel.ExcelUtil;
import com.example.warehouse.sys.pojo.entity.SysDept;
import com.example.warehouse.sys.pojo.entity.SysUser;
import com.example.warehouse.sys.pojo.param.AddNewDeptParam;
import com.example.warehouse.sys.pojo.param.DeptUpdateInfoParam;
import com.example.warehouse.sys.pojo.vo.DeptListItemVO;
import com.example.warehouse.sys.pojo.vo.DeptStandardVO;
import com.example.warehouse.sys.service.IDeptService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/dept")
@Api(tags = "5.1. 部门管理")
@Validated
public class DeptController {
    @Autowired
    private IDeptService deptService;


    @ApiOperation("新增部门")
    @ApiOperationSupport(order = 110)
    @PostMapping("/add-new")
   // @PreAuthorize("hasAuthority('/sys/dept/add-new')")
    public JsonResult addNew(@Validated AddNewDeptParam addNewDeptParam){
        deptService.addNew(addNewDeptParam);
        return JsonResult.ok();
    }

    @ApiOperation("删除部门")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/delete")
   // @PreAuthorize("hasAuthority('/sys/dept/delete')")
    public JsonResult delete(@PathVariable @Range(min = 1, message = "删除标签失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【删除标签】的请求，参数：{}", id);
        deptService.delete(id);
        return JsonResult.ok();
    }
    @ApiOperation("修改部门")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    //@PreAuthorize("hasAuthority('/sys/dept/update/info')")
    public JsonResult updateInfoById(@PathVariable Long id,@Validated DeptUpdateInfoParam deptUpdateInfoParam) {

        deptService.updateInfoById(id,deptUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【根据ID查询类别详情】的请求，参数：{}", id);
        DeptStandardVO queryResult = deptService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }
    @ApiOperation("根据部门列表")
    @ApiOperationSupport(order = 421)
    @GetMapping("")
   // @PreAuthorize("hasAuthority('/sys/dept/list-by-parent')")
    public JsonResult list(Integer page, String queryType,
                           @AuthenticationPrincipal @ApiIgnore CurrentPrincipal principal){
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<DeptListItemVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = deptService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = deptService.list( pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @ApiOperation("部门数据导出")
    @ApiOperationSupport(order = 430)
    @GetMapping("/export")
    public void export(HttpServletResponse response)
            throws IOException {
        String fileName = "部门数据表";
        List<SysDept> list = deptService.selectAll();
        ExcelUtil.download(response, fileName, SysDept.class,list
        );
    }
}
